package demo.android.com.mvpdemo.RxJava;

import android.graphics.Bitmap;
import android.util.Log;

/**
 * Created by Vya on 2018/1/24 0024.
 *  被观察者
 */

public abstract class Observable<T>  implements ObservableSource<T> {

    public static <T> Observable<T> just(T item){
            return onAssembly(new ObservableJust<T>(item));
    }

    private static <T> Observable<T> onAssembly(Observable<T> source) {
        return source;
    }

    @Override
    public void subscribe(Observer<T> observer) {
        Log.e("wangyan","Observable---subscribe-->333333");
        subscribeActual(observer); //这里的Observer是Consumer转化过来的
    }

    public void subscribe(Consumer<T> onNext,int type){
        Log.e("wangyan","Observable---subscribe-->11111---type==="+type);
        subscribe(onNext,null,null);
    }

    private void subscribe(Consumer<T> onNext, Consumer<T> error, Consumer<T> complete) {
        Log.e("wangyan","Observable---subscribe-->22222");
        subscribe(new LambdaObserver<T>(onNext)); //把Consumer转化成Observer
    }

    protected abstract void subscribeActual(Observer<T> observer);

    public <R> Observable<R> map(Function<T, R> function) {
        return  onAssembly(new ObserverbleMap<>(this,function));  //对function进行包装
     }

    public Observable<Bitmap> subscribeOn(Schedulers schedulers) {
        return onAssembly(new ObservableSchedulers(this,schedulers));
    }

    public Observable<T> observerOn(Schedulers schedulers) {
        return onAssembly(new ObserverOnObservable(this,schedulers));
    }
}
